package com.mt.fpb.controller;

import com.alibaba.fastjson.JSON;
import com.mt.fpb.common.exception.GlobalException;
import com.mt.fpb.common.util.GLOBALVARIABLE;
import com.mt.fpb.mapper.TAirMonitorRecordMapper;
import com.mt.fpb.mapper.TWarningMapper;
import com.mt.fpb.mapper.TWaterMonitorRecordMapper;
import com.mt.fpb.model.TAirMonitorRecord;
import com.mt.fpb.model.TWarning;
import com.mt.fpb.model.TWaterMonitorRecord;
import com.mt.fpb.model.vo.CommonResult;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * 环境保护数据接口推送 LT
 * 2021年6月15日15:11:09
 */
@RestController
public class EnvironmentalPush {
    @Resource
    private TWarningMapper tWarningMapper;
    @Resource
    private TAirMonitorRecordMapper tAirMonitorRecordMapper;
    @Resource
    private TWaterMonitorRecordMapper tWaterMonitorRecordMapper;

    /**
     * 接收空气数据入库 LT
     * 2021年6月15日15:11:12
     *
     * @param json
     */
    @PostMapping("/AirPush/getDataFlow")
    public CommonResult AirPush(@RequestBody String json) throws GlobalException {
        //解析map
        if (StringUtils.isEmpty(json)) {
            throw new GlobalException("请求数据为空", 500);
        }
        //获取空气数据
        List<TAirMonitorRecord> tAirMonitorRecords = JSON.parseArray(JSON.parseObject(json).getString("data"), TAirMonitorRecord.class);
        //校验空气数据
        checkTAirMonitorRecords(tAirMonitorRecords);
        //插入空气数据表
        tAirMonitorRecordMapper.insertList(tAirMonitorRecords);
        return CommonResult.success("ok");
    }

    /**
     * 接受水数据
     *
     * @param json
     * @return
     * @throws GlobalException
     */
    @GetMapping("/WaterPush/getDataFlow")
    public CommonResult WaterPush(@RequestBody String json) throws GlobalException {
        //解析map
        if (StringUtils.isEmpty(json)) {
            throw new GlobalException("请求数据为空", 500);
        }
        //获取水数据
        List<TWaterMonitorRecord> tAirMonitorRecords = JSON.parseArray(JSON.parseObject(json).getString("data"), TWaterMonitorRecord.class);
        //校验水数据
        checkTWaterMonitorRecords(tAirMonitorRecords);
        //插入水数据表
        tWaterMonitorRecordMapper.insertList(tAirMonitorRecords);

        return CommonResult.success("ok");
    }

    /**
     * 校验空气数据 LT
     * 2021年6月15日15:11:03
     *
     * @param list
     */
    public void checkTAirMonitorRecords(List<TAirMonitorRecord> list) {
        List<TWarning> tWarningList = new LinkedList<>();
        //遍历
        for (TAirMonitorRecord obj : list) {
            tWarningList.add(initTwarningAir(obj));
        }
        tWarningMapper.insertList(tWarningList);

    }

    /**
     * 校验空气数据 LT
     * 2021年6月15日15:11:03
     *
     * @param list
     */
    public void checkTWaterMonitorRecords(List<TWaterMonitorRecord> list) {
        List<TWarning> tWarningList = new LinkedList<>();
        //遍历
        for (TWaterMonitorRecord obj : list) {
            tWarningList.add(initTwarningWat(obj));
        }
        tWarningMapper.insertList(tWarningList);
    }

    /**
     * 初始化告警对象 LT
     * 2021年6月15日15:52:34
     *
     * @param tAirMonitorRecord
     */
    public TWarning initTwarningAir(TAirMonitorRecord tAirMonitorRecord) {
        TWarning tWarning = new TWarning();
        //校验TSP
        if (Integer.parseInt(tAirMonitorRecord.getTsp()) > GLOBALVARIABLE.TSP) {
            //设置告警类型
            tWarning.setWarningType(1);
            //设置添加时间
            tWarning.setInsertTime(new Date());
            //设置矿区ID
            tWarning.setMineId(tAirMonitorRecord.getMineId());
            //设置处理类型
            tWarning.setWarningStatus(GLOBALVARIABLE.NOTDISPOSE);
            //设置告警时间
            tWarning.setWarningTime(new Date());
            //校验噪音
        } else if (Integer.parseInt(tAirMonitorRecord.getNoise()) > GLOBALVARIABLE.NOISE) {
            //设置告警类型
            tWarning.setWarningType(7);
            //设置添加时间
            tWarning.setInsertTime(new Date());
            //设置矿区ID
            tWarning.setMineId(tAirMonitorRecord.getMineId());
            //设置处理类型
            tWarning.setWarningStatus(GLOBALVARIABLE.NOTDISPOSE);
            //设置告警时间
            tWarning.setWarningTime(new Date());
            //校验PM2.5
        } else if (Integer.parseInt(tAirMonitorRecord.getPm25()) > GLOBALVARIABLE.PM25) {
            //设置告警类型
            tWarning.setWarningType(14);
            //设置添加时间
            tWarning.setInsertTime(new Date());
            //设置矿区ID
            tWarning.setMineId(tAirMonitorRecord.getMineId());
            //设置处理类型
            tWarning.setWarningStatus(GLOBALVARIABLE.NOTDISPOSE);
            //设置告警时间
            tWarning.setWarningTime(new Date());
        }
        return tWarning;
    }

    /**
     * 初始化对象 LT
     *
     * @param tWaterMonitorRecord
     * @return
     */
    public TWarning initTwarningWat(TWaterMonitorRecord tWaterMonitorRecord) {
        TWarning tWarning = new TWarning();
        //校验PH值
        if (Integer.parseInt(tWaterMonitorRecord.getPh()) > GLOBALVARIABLE.PH) {
            //设置告警类型
            tWarning.setWarningType(8);
            //设置添加时间
            tWarning.setInsertTime(new Date());
            //设置矿区ID
            tWarning.setMineId(tWaterMonitorRecord.getMineId());
            //设置处理类型
            tWarning.setWarningStatus(GLOBALVARIABLE.NOTDISPOSE);
            //设置告警时间
            tWarning.setWarningTime(new Date());
            //校验化学需氧量
        } else if (Integer.parseInt(tWaterMonitorRecord.getChemicalOxygenDemand()) > GLOBALVARIABLE.CHEMICAL_OXYGEN_DEMAND) {
            //设置告警类型
            tWarning.setWarningType(9);
            //设置添加时间
            tWarning.setInsertTime(new Date());
            //设置矿区ID
            tWarning.setMineId(tWaterMonitorRecord.getMineId());
            //设置处理类型
            tWarning.setWarningStatus(GLOBALVARIABLE.NOTDISPOSE);
            //设置告警时间
            tWarning.setWarningTime(new Date());
            //校验总氨
        } else if (Integer.parseInt(tWaterMonitorRecord.getTotalNitrogen()) > GLOBALVARIABLE.TOTAL_NITROGEN) {
            //设置告警类型
            tWarning.setWarningType(10);
            //设置添加时间
            tWarning.setInsertTime(new Date());
            //设置矿区ID
            tWarning.setMineId(tWaterMonitorRecord.getMineId());
            //设置处理类型
            tWarning.setWarningStatus(GLOBALVARIABLE.NOTDISPOSE);
            //设置告警时间
            tWarning.setWarningTime(new Date());
            //校验氨氮
        } else if (Integer.parseInt(tWaterMonitorRecord.getAmmoniaNitrogen()) > GLOBALVARIABLE.AMMONIA_NITROGEN) {
            //设置告警类型
            tWarning.setWarningType(11);
            //设置添加时间
            tWarning.setInsertTime(new Date());
            //设置矿区ID
            tWarning.setMineId(tWaterMonitorRecord.getMineId());
            //设置处理类型
            tWarning.setWarningStatus(GLOBALVARIABLE.NOTDISPOSE);
            //设置告警时间
            tWarning.setWarningTime(new Date());
            //校验总磷
        } else if (Integer.parseInt(tWaterMonitorRecord.getTotalPhosphorus()) > GLOBALVARIABLE.TOTAL_PHOSPHORUS) {
            //设置告警类型
            tWarning.setWarningType(12);
            //设置添加时间
            tWarning.setInsertTime(new Date());
            //设置矿区ID
            tWarning.setMineId(tWaterMonitorRecord.getMineId());
            //设置处理类型
            tWarning.setWarningStatus(GLOBALVARIABLE.NOTDISPOSE);
            //设置告警时间
            tWarning.setWarningTime(new Date());
            //校验污水
        } else if (Integer.parseInt(tWaterMonitorRecord.getWasteWater()) > GLOBALVARIABLE.WASTE_WATER) {
            //设置告警类型
            tWarning.setWarningType(13);
            //设置添加时间
            tWarning.setInsertTime(new Date());
            //设置矿区ID
            tWarning.setMineId(tWaterMonitorRecord.getMineId());
            //设置处理类型
            tWarning.setWarningStatus(GLOBALVARIABLE.NOTDISPOSE);
            //设置告警时间
            tWarning.setWarningTime(new Date());
        }
        return tWarning;
    }


}
